package com.lp.business.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.iframework.commons.domain.enums.RecordStatus;
import org.iframework.commons.util.MD5Util;
import org.iframework.commons.util.fast.L;
import org.iframework.commons.util.fast.V;
import org.iframework.support.spring.hibernate.dao.BaseHibernateDaoSupport;
import org.iframework.support.spring.hibernate.service.BaseServiceSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.lp.business.dao.UserDao;
import com.lp.business.model.Role;
import com.lp.business.model.User;
import com.lp.business.service.RoleService;
import com.lp.business.service.UserService;

/**
 * 系统管理员用户服务接口实现类<br>
 * 说明：支持方法查阅实现接口
 * 
 * @author LIPENG
 * 
 */
@Service("userService")
public class UserServiceImpl extends BaseServiceSupport<User, String> implements UserService {

	@Resource(name = "userDao")
	@Override
	public void setBaseHibernateDaoSupport(
			@Qualifier("userDaoImpl") BaseHibernateDaoSupport<User, String> baseHibernateDaoSupport) {
		this.baseHibernateDaoSupport = baseHibernateDaoSupport;
	}
	
	@Autowired
	private RoleService roleService;

	@Override
	public User findUserByLoginNameAndLoginPwd(String loginName, String loginPwd) {
		return ((UserDao) this.baseHibernateDaoSupport).findUserByLoginNameAndLoginPwd(loginName, MD5Util.MD5Encode(loginPwd,1));
	}
	
	@Override
	public boolean isExistsLoginName(String loginName){
		return ((UserDao) this.baseHibernateDaoSupport).isExistsLoginName(loginName);
	}

	@Override
	public List<User> findUserListByRoleId(String roleId) {
		Role role=this.roleService.get(roleId);
		return role!=null?role.getUsers():null;
	}

	@Override
	public User findUserByAccountOpenId(String accountOpenId) {
		if(V.isEmpty(accountOpenId)){
			return null;
		}
		User user = new User();
		user.setAccountOpenId(accountOpenId);
		user.setStatus(RecordStatus.AVAILABLE);
		List<User> findByModel = this.baseHibernateDaoSupport.findByModel(user, null, null);
		if(V.isEmpty(findByModel)){
			return null;
		}else if(findByModel.size()>1){
			return null;
		}
		User u = findByModel.get(0);
		return u;
	}

	@Override
	public User findUserByMoible(String mobile) {
		return ((UserDao) this.baseHibernateDaoSupport).findUserByMoible(mobile);
	}

	@Override
	public boolean saveOrUpdateUser(User user) {
		boolean flag = false;
		if (V.isEmpty(user)) {
			L.i("user为空，保存用户失败");
			return false;
		}
		String mobile = user.getMobile();
		if (V.isEmpty(mobile)) {
			L.i("mobile为空，保存用户失败");
			return false;
		}
		//根据innjiaOpenId查找用户，没有则创建，有则更新
		User find = this.findUserByMoible(mobile);
		if(V.isNotEmpty(find)){
			//更新用户以及用户类型
			user.setId(find.getId());
			flag = this.update(user);
		}else{
			if(V.isNotEquals(RecordStatus.DELETE.name(),user.getStatus().name())){
				//保存用户
				flag = V.isEmpty(this.save(user));
			}
		}
		return flag;
	}
	
	
}
